using System;
using System.Reflection;
using Glue.Internals.Nulls;

namespace Glue.Internals
{
    internal partial class AccessorFactory
    {
        internal class FieldAccessor : IObjectMemberAccessor
        {
            private readonly FieldInfo fieldInfo;

            public String Name { get; private set; }
            public bool CanGetValues { get { return true; } }
            public bool CanSetValues { get { return true; } }
            public Type PropertyType { get; private set; }

            public FieldAccessor(FieldInfo fieldInfo)
            {
                this.fieldInfo = fieldInfo;
                Name = fieldInfo.Name;
                PropertyType = fieldInfo.FieldType;
            }

            public Maybe InvokeGet(object toInvokeOn)
            {
                return fieldInfo.GetValue(toInvokeOn).AsMaybe();
            }

            public void InvokeSet(object toInvokeOn, object toSet)
            {
                fieldInfo.SetValue(toInvokeOn, toSet);
            }

            public bool CanMapFrom(object thisObject) { return true; }
        }
    }
}